package com.asia5b.a5mall.shop.activity

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.logic.viewmodel.promotion.CouponHallActivityVM
import com.asia5b.a5mall.middleware.model.base.ResultModel
import com.asia5b.a5mall.middleware.model.promotion.CouponHallModel
import com.asia5b.a5mall.middleware.utils.newEmptyView
import com.asia5b.a5mall.shop.adapter.CouponHallAdapter
import com.asia5b.a5mall.shop.databinding.ActivityCouponhallBinding
import com.scwang.smartrefresh.layout.api.RefreshLayout
import kotlinx.android.synthetic.main.activity_couponhall.*

/**
 * Created by zhangyuncai on 2020/1/16.
 * 领券中心
 */
@Route(path = ActCons.CouponHallActivity)
class CouponHallActivity : SimpleActivity<CouponHallActivityVM, ActivityCouponhallBinding>() {
    override val viewModelClazz: Class<CouponHallActivityVM>
        get() = CouponHallActivityVM::class.java

    override fun getDataBinding(): ActivityCouponhallBinding {
        return ActivityCouponhallBinding.inflate(layoutInflater)
    }

    override var sIsDark = true
    val mRecyclerView by lazy { rv_couponhall }
    val mDatas = arrayListOf<CouponHallModel>()
    val mAdapter by lazy { CouponHallAdapter(mDatas) }

    override fun initView() {
        mSmartRefreshLayout = mDataBinding.autoCouponhall
        mDataBinding.autoCouponhall?.setEnableRefresh(false)
        mRecyclerView.run {
            layoutManager = LinearLayoutManager(mActivity)
            adapter = mAdapter
        }
        getDatas(0)
    }

    override fun initEvent() {
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val model = mAdapter.getItem(position) as CouponHallModel
            mViewModel.getCoupon(model.coupon_id)
        }
    }

    override fun startObserver() {
        mViewModel.getDatasResultLiveData.observe(this, Observer {
            getDatasResult(it)
        })
    }

    override fun initData() {

    }

    fun getDatas(refresh: Int) {
        mViewModel.getDatas(refresh)
    }

    fun getDatasResult(resultModel: ResultModel<ArrayList<CouponHallModel>>) {
        if (resultModel.page <= 1) {
            mDatas.clear()
        }
        if (!resultModel.data.isNullOrEmpty()) {
            mAdapter.addData(resultModel.data!!)
        }
        mDataBinding.autoCouponhall.setEnableLoadMore(resultModel.page < resultModel.sumPage)
        mAdapter.setEmptyView(newEmptyView(mActivity))
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        getDatas(2)
    }
}